var fs = require('fs');
module.exports = function(app){

app.get('/app/createLocation', function(req, res) {
	var CountryObject = AV.Object.extend("DHCountry");
	var query = new AV.Query(CountryObject);
	
	query.find({
	  success : function(results){
	  		res.render('create_location', { CountryObjects : results });
	  },
	  error: function(error) {
		res.render('error', { message: error.message });
		console.log('signin false: %j', error.code +" : "+ error.message);	
	  }
	});	    
});

app.get('/app/createLocationNew', function(req, res) {
	res.render('create_location_new');
});

app.post('/app/createLocation', function(req, res) {
    var currentUser = AV.User.current();
	var LocationObject = AV.Object.extend("DHLocation");
	var locationObj = new LocationObject();
	
	var latitude = req.param('latitude');
	var longitude = req.param('longitude');
	console('经度%j, 纬度%j', latitude, longitude);
	
	var point = new AV.GeoPoint(parseFloat(latitude), parseFloat(longitude));
	locationObj.set("geoPoint", point);
	
	locationObj.set("locationName", req.param('mylocation'));

	var countryObj = AV.Object.createWithoutData("DHCountry", req.param('country'));
	locationObj.set("country", countryObj);
	locationObj.set("district", req.param('district'));
	//locationObj.set("temperature", parseInt(req.param('temperature')));
	locationObj.set("briefing", req.param('briefing'));
	
	var createrObj = AV.Object.createWithoutData("_User", currentUser.id);
	locationObj.set("creater", createrObj);
	
	locationObj.set("startTime", req.param('startTime'));
	locationObj.set("endTime", req.param('endTime'));
	locationObj.set("recommend", parseInt(req.param('recommend')));
		
	var taskFile = req.files.imageFile;

	if(taskFile.size > 0){
		fs.readFile(taskFile.path, function(err, data){
		  if(err){return res.send("读取文件失败");}
		  var base64Data = data.toString('base64');
		  var avFile = new AV.File(taskFile.name, {base64: base64Data});
		  avFile.save().then(function(avFile){
			locationObj.set("imageFile", avFile);
			locationObj.save(null, {
				success: function(locationObj) {
					res.render('success', { message: '保存成功！' });
				},
				error: function(locationObj, error) {
					res.render('error', { message: error.message });
					console.log('signin false: %j', error.code +" : "+ error.message);
				}
			});				
		  });
		});			
	
	} else {
		locationObj.save(null, {
			success: function(locationObj) {
				res.render('success', { message: '保存成功！' });
			},
			error: function(locationObj, error) {
				res.render('error', { message: error.message });
				console.log('signin false: %j', error.code +" : "+ error.message);
			}
		});	
	}		
	
});

app.get('/app/openLocation', function(req, res) {

var LocationObj = AV.Object.extend("DHLocation");
var query = new AV.Query(LocationObj);
query.include('country');
query.get(req.param("locationId"), {
  success: function(locationObj) {
  
	var CountryObject = AV.Object.extend("DHCountry");
	var countryQuery = new AV.Query(CountryObject);
	
	countryQuery.find({
		success: function(results) {												
			var imageFile = locationObj.get("imageFile");	
			var geoPoint = locationObj.get("geoPoint");
			
			if(imageFile != null){
				var url = imageFile.url();
				res.render('update_location', { imageFileSrc: url,LocationObject: locationObj, CountryResults: results, GeoPoint : geoPoint });			
			} else {					
				res.render('update_location', { imageFileSrc: "", LocationObject: locationObj, CountryResults: results, GeoPoint : geoPoint });					
			}
		
		  },
		  error: function(error) {
			res.render('error', { message: error.message });
			console.log('signin false: %j', error.code +" : "+ error.message);	
		  }
		});				
  },
  error: function(LocationObj, error) {
	res.render('error', { message: error.message });
	console.log('signin false: %j', error.code +" : "+ error.message);
  }
});			
		
	
});

app.get('/app/locationList', function(req, res) {
	var page = 0;
	if(req.param("page") != undefined){
		page = parseInt(req.param("page"));
	}
	var nextPage = page + 1;
	var previousPage = page - 1;
	var skip = page * 10;
	
	var LocationObject = AV.Object.extend("DHLocation");
	var query = new AV.Query(LocationObject);
	query.include("creater")
	
	query.skip(skip);
	query.limit(10);	
	query.find({
		success: function(results) {			
			if(results.length > 0){
				res.render('location_list', { Locations : results, next: nextPage, previous: previousPage});
			} else {
				res.render('location_list', { Locations : results, next: 0, previous: previousPage});
			}
		},
		error: function(error) {
			res.render('error', { message: error.message });
			console.log('signin false: %j', error.code +" : "+ error.message);	
		}
	});			
	
});

app.get('/app/locationListNew', function(req, res){
	res.render('location_list_new');
});

app.post('/app/deleteLocation', function(req, res) {

	var locationObj = AV.Object.createWithoutData("DHLocation", req.param('locationId'));
		locationObj.destroy({
		  success: function(locationObj) {
			// The object was deleted from the AVOS Cloud.
			res.send('success');
		  },
		  error: function(locationObj, error) {
			res.send(error.message);
			console.log('signin false: %j', error.code +" : "+ error.message);	
		  }
	});
	
});

app.get('/app/myLocationList', function(req, res) {
	var currentUser = AV.User.current();
	
	var page = 0;
	if(req.param("page") != undefined){
		page = parseInt(req.param("page"));
	}
	var nextPage = page + 1;
	var previousPage = page - 1;
	var skip = page * 10;
	
	var LocationObject = AV.Object.extend("DHLocation");
	var query = new AV.Query(LocationObject);
	query.equalTo("creater", currentUser.id);
	query.skip(skip);
	query.limit(10);	
	query.find({
		success: function(results) {
			if(results.length > 0){
				res.render('location_list', { Locations : results, next: nextPage, previous: previousPage});
			} else {
				res.render('location_list', { Locations : results, next: 0, previous: previousPage});
			}			
		},
		error: function(error) {
			res.render('error', { message: error.message });	
		}
	});			
	
});

app.post('/app/updateLocation', function(req, res) {
    var currentUser = AV.User.current();
    console.log('locationid:%j', req.param('locationId'));
	var locationObj = AV.Object.createWithoutData("DHLocation", req.param('locationId'));
	
	var latitude = req.param('latitude');
	var longitude = req.param('longitude');
	var point = new AV.GeoPoint(parseFloat(latitude), parseFloat(longitude));
	locationObj.set("geoPoint", point);
	
	locationObj.set("locationName", req.param('taskTitle'));
	
	var countryObj = AV.Object.createWithoutData("DHCountry", req.param('country'));
	locationObj.set("country", countryObj);
	locationObj.set("district", req.param('district'));
	locationObj.set("briefing", req.param('briefing'));
	
	locationObj.set("startTime", req.param('startTime'));
	locationObj.set("endTime", req.param('endTime'));
		
	locationObj.set("recommend", parseInt(req.param('recommend')));
	
	var taskFile = req.files.imageFile;
	
	if(taskFile != undefined && taskFile.size > 0){	
			
		fs.readFile(taskFile.path, function(err, data){
		  if(err){return res.send("读取文件失败");}
		  var base64Data = data.toString('base64');
		  var avFile = new AV.File(taskFile.name, {base64: base64Data});
		  avFile.save().then(function(avFile){
			locationObj.set("imageFile", avFile);
			locationObj.save(null, {
				success: function(locationObj) {
					res.render('success', { message: '操作成功！' });
				},
				error: function(locationObj, error) {
					res.render('error', { message: error.message });
					console.log('signin false: %j', error.code +" : "+ error.message);
				}
			});				
		  });
		});			
	
	} else {
	 	
		locationObj.save(null, {
			success: function(locationObj) {
				res.render('success', { message: '操作成功！' });
			},
			error: function(locationObj, error) {
				res.render('error', { message: error.message });
				console.log('signin false: %j', error.code +" : "+ error.message);
			}
		});	
	}
	
			
});

};
